﻿/*
该js用来解析流程图为 数组 结构如下:
    arrs[0] = "actId";
    arrs[1] = "actName";
    arrs[2] = "type";
    arrs[3] = "out";
    arrs[4] = "in";
    arrs[5] = "express";
    arrs[6] = "users";
    arrs[7] = "nextAct";
    arrs[8]="module";
*/
function $id(ele) {
    return document.getElementById(ele);
}

function savePorcess() {
    wfparse();
}
function wfparse() {
    //---------------------获取活动集合---------------------------
    var wfacts = document.getElementsByTagName("wfact");
    //---------------------获取路径集合--------------------------
    var wfpaths = document.getElementsByTagName("wfpath");
    //---------------------获取条件集合--------------------------
    var wfcons = document.getElementsByTagName("wfcondition");
    //---------------------获取授权用户集合----------------------
    var wfusers = document.getElementsByTagName("wfuser");
    
    /*
     * 解析
     */
    //先判断是 开始活动==1 结束活动>=1
    var actCount = wfacts.length;
    var startNode = 0, endNode = 0;
    var pathCount = wfpaths.length;
    var matchedPathCount = 0;
    //--------------先检测path中是否有自己调用自己的点
    for (var n = 0; n < pathCount; n++) {
        if (wfpaths[n].getAttribute("start") == wfpaths[n].getAttribute("end")) {
            alert("存在 自己调用自己回路 !");
            return;
        }
    }
    //-----------检查孤立点---同时把节点存为数组,供后台使用
    for (var i = 0; i < actCount; i++) {

        var isEnd = false;
        if (wfacts[i].getAttribute("wftype") == "actEnd" || wfacts[i].getAttribute("wftype") =="actRollback") {
            endNode++;
            isEnd = true;
        }
        /*
       首先,点肯定是不会凭空增多或减少的,因为以node为标准
       检查是否有孤立点 是否有回路等
       除开始节点外 其他节点都有一个前置节点
       */
        var hasPreAct = false;
        var hasMatched = false;
        var hasAfter = false;
        var test = "";
        if (i == 18) {
            var k = i;
        }
        for (var n = 0; n < pathCount; n++) {

            if (i == 18) {
                var k = i;
                test += wfpaths[n].getAttribute("end") + ";";
            }
            //-----------是否有前置节点
            if (wfacts[i].id == wfpaths[n].getAttribute("end") ) {
                hasMatched = true;
                hasPreAct = true;
            }
            //-------是否有后置节点(主要关于:结尾节点)
            if (wfacts[i].id == wfpaths[n].getAttribute("start")) {
                if (isEnd) {
                    alert("结束活动不允许再有后续活动 !");
                    return;
                }
                hasAfter = true;
            }
        }//end inner for loop
        
        //---------if act is not the end act then check wether it has after act
        if (!hasAfter && !isEnd) {
            alert("存在孤立活动集 !");
            return;
        }
        //-----------is start node , otherwise ..
        if (hasPreAct == false) {
            if (wfacts[i].getAttribute("wftype") == "actStart") {
                startNode++;
            }
            else {
                alert("存在孤立活动集 !");
                return;
            }
        }
        
        //-----是否有匹配的节点和路径
        if (hasMatched) {
            matchedPathCount++;
        }
    }
    if (startNode != 1) {
        alert("开始节点有 且只有 一个 !");
        return;
    }
    if (endNode < 1) {
        alert("结束节点至少需要一个 !");
        return;
    }
    if (matchedPathCount != actCount-1) {
        alert("存在孤立节点或节点集 !");
        return;
    }
    
    //---------------------------组织格式(table)-------------------
    /*
     第一行为表头
    */
    var arrs = new Array();
    //-----增加一行表头
    arrs[0] = "actId";
    arrs[1] = "actName";
    arrs[2] = "mainType";
    arrs[3] = "type";
    arrs[4] = "out";
    arrs[5] = "in";
    arrs[6] = "express";
    arrs[7] = "users";
    arrs[8] = "nextAct";
    arrs[9] = "module";
    //--------填充数据-------------------------
    var span = 10;
    for (var n = 0; n < pathCount; n++) {
        var actid = wfpaths[n].getAttribute("start");
        arrs[0 + span * (n + 1)] = actid;//actid
        

        var localTempName = "";
        for (var i = 0; i < wfacts.length; i++) {
            if (wfacts[i].id == actid) {
                //-------------act type
                arrs[2 + span * (n + 1)] = "";//main type
                arrs[3 + span * (n + 1)] = wfacts[i].getAttribute("wftype");//sub type
                //-------------------act name------------
                if (wfacts[i].getAttribute("wftype") == "actCommon") {
                    arrs[1 + span * (n + 1)] = wfacts[i].innerHTML;
                } else {
                    arrs[1 + span * (n + 1)] = wfacts[i].getAttribute("wfname");
                }//end inner act name
                //----------------in or out----
                arrs[4 + span * (n + 1)] = wfacts[i].getAttribute("wfout");//output
                arrs[5 + span * (n + 1)] = wfacts[i].getAttribute("wfin");//in
                
                //-----------------module
                arrs[9 + span * (n + 1)] = wfacts[i].getAttribute("wfmodule");//module
                break;
            }//end outer for
        }//end inner loop
        
        var localUsersCount = wfusers.length;
        for (var i = 0; i < localUsersCount; i++) {
            if (wfusers[i].getAttribute("wfactid") == actid) {
                arrs[7 + span * (n + 1)] = wfusers[i].innerHTML.split("{")[0];//users
                break;
            }
        }
        arrs[8 + span * (n + 1)] = wfpaths[n].getAttribute("end");//next act
        
        var localExpressCount = wfcons.length;
        for (var i = 0; i < localExpressCount; i++) {
            if ("wfp_"+wfcons[i].getAttribute("wfpath") == wfpaths[n].id) {
                arrs[6 + span * (n + 1)] = wfcons[i].innerHTML;//express
                break;
            }
        }
    }//end outer for loop
   
    //-----------对于结束活动 因为没有后置活动,所以 肯定就没有路径了 在此 假设结束活动的后置活动为null
    var localCurIndex = arrs.length;
    var ident = 0;
    for (var i = 0; i < actCount; i++) {
        var tempType = wfacts[i].getAttribute("wftype");
        if (tempType== "actEnd" || tempType=="actRollback") {
            arrs[0 + span * ident + localCurIndex] = wfacts[i].id; //actid
            arrs[1 + span * ident + localCurIndex] =wfacts[i].getAttribute("wfname"); //act name
            arrs[2 + span * ident + localCurIndex] = ""; //main type
            arrs[3 + span * ident + localCurIndex] = tempType; //sub type
            arrs[4 + span * ident + localCurIndex] = "N"; //output
            arrs[5 + span * ident + localCurIndex] ="N"; //in
            arrs[6 + span * ident + localCurIndex] = ""; //express
            arrs[7 + span * ident + localCurIndex] = ""; //users
            arrs[8 + span * ident + localCurIndex] = ""; //next act
            arrs[9 + span * ident + localCurIndex] = wfacts[i].getAttribute("wfmodule"); //module
            ident++;
        }
    }
    //---------保存----------

    var identp = ns.PraserProcess(arrs, parseHtml()).value;//用户id
    if (identp) alert("保存成功!");
    else {
        alert("保存失败!");
    }
}

function parseHtml() {
    var htmls = $id("mainCanvas");
    var connectors = htmls.getElementsByClassName("connector");
    var imgs = htmls.getElementsByClassName("connector-end");

    var img = "";
    if (imgs != null && imgs.length > 0) img = "<img src=\"Images/arrow.gif\" class=\"connector-end\" />";
    imgs = null;

    var count = (connectors == null) ? 0 : connectors.length;
    if (count == 0) return "";
    
    /*
        重新构建路径以保存数据库
        <div class='connector'>
            <image ../>
        </div>
    */
    
    var htmPaths = "";
    var curPathId = "";
    for (var i = 0; i < count; i++) {
        var pathid = connectors[i].id.substr(0, connectors[i].id.lastIndexOf("_"));
        if (curPathId == pathid) continue;
        //----------------构建新路径
        var pathStr = connectors[i].outerHTML;
        pathStr = pathStr.replace("</div>", "").replace(connectors[i].id, pathid);
        pathStr += img;
        pathStr += "</div>";
        htmPaths += pathStr;
        curPathId = pathid;
    }
    htmPaths = " <div class=\"canvas\" id=\"mainCanvas\" style='width: 2000px; height: 2000px; overflow: auto; position: relative; background-image: url(\"Images/bck.png\"); background-color: rgb(245, 245, 245);'>" + $id("mainCanvas_innerDiv").outerHTML + htmPaths+"</div>";
    htmPaths += $id("id_div_conditions_cols").outerHTML;
    htmPaths += $id("id_idv_users_cols").outerHTML;
    htmPaths += $id("id_div_paths_cols").outerHTML;
    return htmPaths;
}